<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            label-width="auto"
            v-show="showSearch"
        >
            <el-row>
            <el-form-item label="生产类别" prop="type">
                    <el-select
                        style="width: 180px"
                        filterable
                        v-model="queryParams.type"
                        placeholder="请选择生产类别"
                    >
                        <el-option
                            v-for="item in typeOptions"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="生产单编码" prop="produceNo">
                    <el-input
                        v-model.trim="queryParams.produceNo"
                        style="width: 180px"
                        placeholder="请输入生产单编码"
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="产品名称" prop="productName">
                    <el-input
                        v-model.trim="queryParams.productName"
                        style="width: 180px"
                        placeholder="请输入产品名称"
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <div class="operation">
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    <el-button @click="advanced = !advanced">
                        <a style="color: #448ef7">
                            <el-icon v-if="advanced">
                                <ArrowUpBold />
                            </el-icon>
                            <el-icon v-else>
                                <ArrowDownBold />
                            </el-icon>
                        </a>
                    </el-button>
                </div>
            </el-row>
            <el-row v-if="advanced">
                <el-form-item label="状态" prop="status">
                    <el-select
                        style="width: 180px"
                        filterable
                        v-model="queryParams.status"
                        placeholder="请选择状态"
                    >
                        <el-option
                            v-for="item in ems_order_status"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="定量" prop="weight">
                    <el-input
                        v-model.trim="queryParams.weight"
                        style="width: 180px"
                        placeholder="请输入定量"
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="下单日期" prop="submitDate">
                    <el-date-picker
                        style="width: 180px"
                        v-model="queryParams.submitDate"
                        value-format="YYYY-MM-DD "
                        type="date"
                        placeholder="请选择日期"
                    />
                </el-form-item>
                <el-form-item label="日期" style="width: 308px">
                    <el-date-picker
                        v-model="dateRange"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                    ></el-date-picker>
                </el-form-item>
            </el-row>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5"></el-col>
            <right-toolbar
                v-model:showSearch="showSearch"
                @queryTable="getList"
                :columns="columns"
            ></right-toolbar>
        </el-row>
        <div>
            <el-table
                v-loading="loading"
                row-key="produceId"
                :summary-method="getSummaries"
                height="65vh"
                show-summary
                @selection-change="handleSelectionChange"
                border
                :data="dataList"
            >
                <el-table-column
                    type="selection"
                    align="center"
                    width="40"
                />
                <el-table-column label="序号" width="60" type="index" align="center" />
                <el-table-column
                    label="状态"
                    min-width="100"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="status"
                    v-if="columns[0].visible"
                >
                    <template v-slot="{ row }">
                        <dict-tag :options="ems_order_status" :value="row.status" />
                    </template>
                </el-table-column>
                <el-table-column
                    label="生产单编码"
                    width="180"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="orderNo"
                    v-if="columns[1].visible"
                >
                    <template v-slot="{ row }">
                        <el-button type="primary" @click="handleView(row)" text>
                            {{ row.produceNo }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column
                    label="出版编号"
                    min-width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="pubNo"
                    v-if="columns[3].visible"
                ></el-table-column>
                <el-table-column
                    label="产品名称"
                    min-width="160"
                    align="center"
                    prop="productName"
                    v-if="columns[3].visible"
                ></el-table-column>
                <el-table-column
                    label="订单数量"
                    width="100"
                    align="center"
                    prop="orderCount"
                    v-if="columns[3].visible"
                ></el-table-column>
                <el-table-column label="书号" width="120" align="center" prop="bookNo" />
                <el-table-column
                    label="下单日期"
                    min-width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="submitDate"
                    v-if="columns[4].visible"
                >
                    <template v-slot="{ row }">
                        {{ row.submitDate?.split('T')[0] ?? '' }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="交货日期"
                    min-width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="deliverDate"
                    v-if="columns[5].visible"
                >
                    <template v-slot="{ row }">
                        {{ row.deliverDate?.split('T')[0] ?? '' }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="备注"
                    min-width="120"
                    align="center"
                    :show-overflow-tooltip="true"
                    prop="remark"
                >
                    <template v-slot="{ row }">{{ row.changePiece }} {{ row.remark }}</template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="total > 0"
                :total="total"
                v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize"
                @pagination="getList"
            />
        </div>
        <el-divider content-position="left">用纸信息</el-divider>
        <el-table v-loading="paperLoading"   height="65vh" :data="producePaperList" border>
            <el-table-column  label="序号" width="60" type="index" align="center" />
            <!-- <el-table-column
                label="纸张序列号"
                min-width="120"
                prop="paperProduceNo"
                align="center"
            ></el-table-column> -->
            <el-table-column
                label="纸张编码"
                min-width="120"
                prop="paperNo"
                align="center"
            ></el-table-column>

            <el-table-column label="纸张类型" prop="type" width="120" align="center">
                <template #default="scope">
                    <dict-tag :options="mes_paper_material" :value="scope.row.type" />
                </template>
            </el-table-column>
            <el-table-column label="纸张名称" min-width="150" prop="paperName" align="center" />
            <el-table-column label="纸型" width="100" prop="paperType" align="center">
                <template #default="scope">
                    <dict-tag :options="mes_paper_type" :value="scope.row.paperType" />
                </template>
            </el-table-column>
            <el-table-column
                label="定量"
                width="100"
                prop="weight"
                align="center"
            ></el-table-column>
            <el-table-column label="规格" min-width="120" prop="specifications" align="center">
                <template #default="scope">
                    <dict-tag :options="mes_paper_specs" :value="scope.row.specifications" />
                </template>
            </el-table-column>
            <el-table-column
                label="幅宽(mm)"
                width="100"
                prop="length"
                align="center"
            ></el-table-column>
            <el-table-column label="单位" min-width="120" prop="unit" align="center">
                <template #default="scope">
                    <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
                </template>
            </el-table-column>
            <el-table-column
                label="当时库存"
                width="120"
                prop="amount"
                align="center"
            ></el-table-column>
            <el-table-column
                label="数量"
                min-width="120"
                prop="reamAmount"
                align="center"
            ></el-table-column>
            <el-table-column
                label="裁切尺寸"
                width="100"
                prop="size"
                align="center"
            ></el-table-column>
            <!-- <el-table-column
                label="版材尺寸"
                width="100"
                prop="sheetSize"
                align="center"
            > <template #default="scope">
                    <dict-tag :options="ems_machine_info_size" :value="scope.row.sheetSize" />
                </template>
        </el-table-column> -->
        </el-table>
        <el-dialog v-model="open" title="产品信息" width="70%" @close="handleCancel">
            <el-table v-if="type == 1" v-loading="loadingList" :data="produceProductList" border>
                <el-table-column label="序号" width="60" type="index" align="center" />
                <el-table-column
                    label="产品序列号"
                    min-width="120"
                    prop="productNo"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="订单数量"
                    width="120"
                    prop="produceCount"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="书刊编码"
                    min-width="120"
                    prop="bookCode"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="书刊名称"
                    min-width="160"
                    prop="bookName"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="产品品种"
                    prop="breedType"
                    min-width="120"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="清样编号"
                    min-width="120"
                    prop="sampleNo"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="文件号"
                    min-width="120"
                    prop="fileNo"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="书号"
                    min-width="120"
                    prop="bookNo"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="定价"
                    min-width="120"
                    prop="fixPrice"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="组成"
                    min-width="120"
                    prop="composition"
                    align="center"
                ></el-table-column>
                <el-table-column label="印色" min-width="100" prop="printColor" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_order_print_color" :value="scope.row.printColor" />
                    </template>
                </el-table-column>
                <el-table-column label="颜色" width="130" prop="color" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_color" :value="scope.row.color" />
                    </template>
                </el-table-column>
                <el-table-column label="纸型" width="120" prop="paperType" align="center">
                    <template #default="scope">
                        <dict-tag :options="mes_paper_type" :value="scope.row.paperType" />
                    </template>
                </el-table-column>
                <el-table-column
                    label="纸张编码"
                    width="120"
                    prop="paperNo"
                    align="center"
                ></el-table-column>

                <el-table-column label="用纸尺寸" prop="paperSize" width="120" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_paper_size" :value="scope.row.paperSize" />
                    </template>
                </el-table-column>
                <el-table-column label="纸张名称" width="120" prop="paperName" align="center" />
                <el-table-column label="覆膜" width="120" prop="coverFilm" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_cover_film" :value="scope.row.coverFilm" />
                    </template>
                </el-table-column>

                <el-table-column label="喷码" width="100" prop="marking" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_marking" :value="scope.row.marking" />
                    </template>
                </el-table-column>
                <el-table-column
                    label="联版数量"
                    width="120"
                    prop="lianbanCount"
                    align="center"
                ></el-table-column>
                <el-table-column label="内文承印厂" width="120" prop="textPrinter" align="center">
                    <!-- <template #default="scope">
                        <dict-tag :options="produce_text_printer" :value="scope.row.textPrinter" />
                    </template> -->
                </el-table-column>
                <el-table-column
                    label="客户名称"
                    width="120"
                    prop="customerName"
                    align="center"
                ></el-table-column>

                <el-table-column label="科目" width="100" prop="subjectType" align="center">
                    <template #default="scope">
                        <dict-tag
                            :options="ems_workshop_subject_type"
                            :value="scope.row.subjectType"
                        />
                    </template>
                </el-table-column>
            </el-table>
            <el-table
                v-else
                v-loading="loadingList"
                class="myTable"
                :data="produceProductList"
                border
            >
                <el-table-column label="序号" width="60" type="index" align="center" />
                <el-table-column
                    label="订单数量"
                    width="100"
                    prop="produceCount"
                    align="center"
                ></el-table-column>

                <el-table-column
                    label="品种"
                    prop="breedType"
                    width="120"
                    align="center"
                ></el-table-column>
                <el-table-column label="装订方式" width="140" prop="bindingMode" align="center">
                    <template #default="scope">
                        <dict-tag
                            :options="ems_order_binding_mode"
                            :value="scope.row.bindingMode"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="开本数" width="100" prop="bookSize" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_book_size" :value="scope.row.bookSize" />
                    </template>
                </el-table-column>
                <el-table-column
                    label="组成"
                    width="100"
                    prop="composition"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="详细信息"
                    min-width="120"
                    prop="detail"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="贴数信息"
                    width="200"
                    prop="piece"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="印张"
                    width="100"
                    prop="sheet"
                    align="center"
                ></el-table-column>
                <el-table-column label="印色" min-width="100" prop="printColor" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_order_print_color" :value="scope.row.printColor" />
                    </template>
                </el-table-column>
                <el-table-column label="颜色" width="120" prop="color" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_color" :value="scope.row.color" />
                    </template>
                </el-table-column>

                <el-table-column label="用纸尺寸" prop="paperSize" min-width="135" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_produce_paper_size" :value="scope.row.paperSize" />
                    </template>
                </el-table-column>
                <el-table-column label="成品尺寸(mm)" width="130" prop="finishSize" align="center">
                    <template #default="scope">
                        <dict-tag
                            :options="ems_produce_finish_size"
                            :value="scope.row.finishSize"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="版材尺寸" min-width="125" prop="sheetSize" align="center">
                    <template #default="scope">
                        <dict-tag :options="ems_machine_info_size" :value="scope.row.sheetSize" />
                    </template>
                </el-table-column>
                <el-table-column label="纸型" width="120" prop="paperType" align="center">
                    <template #default="scope">
                        <dict-tag :options="mes_paper_type" :value="scope.row.paperType" />
                    </template>
                </el-table-column>
                <el-table-column
                    label="纸张编码"
                    width="120"
                    prop="paperNo"
                    align="center"
                ></el-table-column>
                <el-table-column label="纸张名称" min-width="160" prop="paperName" align="center" />
                <el-table-column
                    label="用版数量"
                    width="100"
                    prop="boardsCount"
                    align="center"
                ></el-table-column>
                <el-table-column
                    label="清样编号"
                    min-width="120"
                    prop="sampleNo"
                    align="center"
                ></el-table-column>

                <el-table-column label="烤版" width="100" prop="kaoban" align="center">
                    <template #default="scope">
                        {{ scope.row.kaoban == 1 ? '是' : scope.row.kaoban == 2 ? '否' : '' }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="产品序列号"
                    min-width="120"
                    prop="productNo"
                    align="center"
                ></el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script setup name="order">
import { downLoad, toNumber } from '@/utils/ruoyi'
import { getProduceOrderList, updatebookNo, getProduceById } from '@/api/production/cover'
const { proxy } = getCurrentInstance()
const loading = ref(true)
const paperLoading = ref(false)
const loadingList = ref(false)
const dataList = ref([])
const showSearch = ref(true)
const total = ref(0)
const advanced = ref(false)
const countTotal = ref(0)
const multiple = ref(true)
const produceProductList = ref([])
const selectBookNo = ref([])
const dateRange = ref()
const open = ref(false)
const type = ref('')
const typeOptions=ref([
    { label:'封面',value:'1'},
     { label:'内文',value:'2'},
])
// 列显隐信息
const columns = ref([
    { key: 0, label: `状态`, visible: true },
    { key: 1, label: `生产单编码`, visible: true },
    { key: 2, label: `新/旧版`, visible: true },
    { key: 3, label: `出版编号`, visible: true },
    { key: 4, label: `下单日期`, visible: true },
    { key: 5, label: `加急交货日期`, visible: true },
    { key: 6, label: `备注`, visible: true },
    { key: 7, label: `车间`, visible: true },
])
const producePaperList = ref([])
const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        // state: 2,
    },
})
const { queryParams } = toRefs(data)
const {
    ems_produce_book_size,
    ems_order_binding_mode,
    ems_machine_info_size,
    ems_produce_finish_size,
    ems_order_print_color,
    ems_produce_color,
    ems_produce_paper_size,
    ems_produce_cover_film,
    ems_produce_marking,
    produce_text_printer,
    mes_paper_type,
    mes_paper_unit,
    mes_paper_specs,
    mes_paper_material,
    ems_workshop_subject_type,
    ems_order_status,
    ems_produce_is_new,
    ems_workshop,
} = proxy.useDict(
    'ems_produce_book_size',
    'ems_order_binding_mode',
    'ems_machine_info_size',
    'ems_produce_finish_size',
    'ems_order_print_color',
    'ems_produce_color',
    'ems_produce_paper_size',
    'ems_produce_cover_film',
    'ems_produce_marking',
    'produce_text_printer',
    'mes_paper_type',
    'mes_paper_unit',
    'mes_paper_specs',
    'ems_order_status',
    'mes_paper_material',
    'ems_workshop_subject_type',
    'ems_produce_is_new',
    'ems_workshop'
)

const selectable = (row, index) => {
    if (row.bookNo) {
        return true
    }
}
const handleSelectionChange = row => {
    let producePaperListPromises = []
    row.forEach(item => {
        producePaperListPromises.push(
            getProduceById({ pid: item.produceId }).then(res => {
                // console.log(res);
                // res.data.producePaperList.forEach(item=>{
                //     res.data.producePaperList.forEach(obj=>{
                //         if(item.orderProductId ==obj.orderProductId){
                //             item.sheetSize = obj.sheetSize 
                //         }
                //     })
                // })
                return  res.data.producePaperList
            })
        )
    })
    paperLoading.value = true
    Promise.all(producePaperListPromises)
        .then(producePaperLists => {
            producePaperList.value = producePaperLists.reduce(
                (acc, paperList) => [...acc, ...paperList],
                []
            )
            paperLoading.value = false
            producePaperList.value = producePaperList.value.reduce((acc, current) => {
                const existingItem = acc.find(item => item.paperNo === current.paperNo)
                if (existingItem) {
                    existingItem.reamAmount += current.reamAmount
                    existingItem.reamAmount = toNumber(existingItem.reamAmount, 4)
                } else {
                    acc.push({ ...current })
                }
                return acc
            }, [])
        })
        .catch(error => {
            console.error('An error occurred:', error)
        })
}

// 合计
const getSummaries = param => {
    const { columns, data } = param
    const sums = []
    columns.forEach((column, index) => {
        if (index === 1) {
            sums[index] = '合计'
            return
        }
        switch (column.property) {
            case 'orderCount':
                sums[index] = countTotal.value
                break
            default:
                break
        }
    })
    return sums
}
const getList = () => {
    loading.value = true
    getProduceOrderList({
        ...queryParams.value,
        startTime: Array.isArray(dateRange.value) ? dateRange.value[0] : undefined,
        endTime: Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
    })
        .then(res => {
            loading.value = false
            dataList.value = res.rows
            countTotal.value = res.rows[0].countTotal
            total.value = res.total
        })
        .catch(err => {
            loading.value = false
        })
}

// 查看
const handleView = row => {
    type.value = row.type
    loadingList.value = true
    getProduceById({ pid: row.produceId }).then(res => {
        produceProductList.value = res.data.produceProductList
        loadingList.value = false
    })
    open.value = true
}
const handleCancel = () => {}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1
    getList()
}
/** 重置按钮操作 */
const resetQuery = () => {
    producePaperList.value = []
    dateRange.value = []
    proxy.resetForm('queryRef')
    handleQuery()
}
getList()
</script>

<style lang="scss" scoped></style>
